<template>
  <div class="product-add pb50">
   <!--添加文章-->
    <el-form size="small" :model="form" ref="form" :rules="rules" label-width="100px">
      <div class="common-form">添加首页商品</div>
      <el-form-item label="商品名称" prop="product_name"><el-input v-model="form.product_name" readonly class="max-w460"></el-input></el-form-item>
      <el-form-item label="图片">
        <div>
          <img :src="form.product_image" class="mt10" width="120px;" />
        </div>
      </el-form-item>
      <el-form-item label="排序"><el-input type="number" v-model="form.sort" placeholder="请输入数字" class="max-w460"></el-input></el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import HomeproductApi from '@/api/homeproduct.js';
export default {
  components: {},
  data() {
    return {
      /*是否上传图片*/
      isupload: false,
      isImg: false,
      path: '',
      /*form表单数据*/
      form: {
        assemble_product_id:'',
        product_name:'',
        product_image:'',
        shop_name:'',
        sort:1
      },
      /*文章类别*/
      catgory: [],
      loading: false,
      /*验证规则*/
      rules: {
      }
    };
  },
  created() {
    Object.assign(this.form, JSON.parse(sessionStorage.getItem('ASSEMBLE_PRODUCT')) );
    
    sessionStorage.removeItem('ASSEMBLE_PRODUCT');
  },
  methods: {
    /*上传*/
    openUpload() {
      this.isupload = true;
    },

    /*获取图片*/
    returnImgsFunc(e) {
      let self = this;
      if (e != null) {
        this.form.image_id = e[0].file_id;
        this.path = e[0].file_path;
        this.isImg = true;
      }
      this.isupload = false;
    },

    /*添加文章*/
    onSubmit() {
      let self = this;
      let form = self.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          HomeproductApi.addProduct(form, true)
            .then(data => {
              self.loading = false;
              self.$message({
                message: data.msg,
                type: 'success'
              });
              self.$router.push('/market/home/index');
            })
            .catch(error => {
              self.loading = false;
            });
        }
      });
    },

    /*取消添加，返回文章列表*/
    cancelFunc() {
      this.$router.push({
        path: '/market/home/index'
      });
    }
  }
};
</script>

<style>
.edit_container {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

</style>
